<?php
namespace app\admin\controller;
use think\facade\Db;
class News extends Base{

    //列表数据
    public function list_data($get=0,$page=1,$search='',$search_type=''){
        $this->assign('search',$search);
        $this->assign('search_type',$search_type);
        $search_type = $search_type ? ['type' => $search_type]:[];
        $search = $search ? [['title', 'like', '%'.$search.'%']]:[];
        $listData = Db::table('an_news')
        // ->fetchSql(true)//输出mysql语句
        ->where($search_type)
        ->where($search)
        ->order('sort', 'dsc')
        ->page($page,10)
        ->select()
        ->toArray(); 
        $totalNum = Db::table('an_news')->where($search_type)->where($search)->count();
        foreach($listData as $k=>$v){
            $listData[$k]['up_time'] = date("Y-m-d G:i",$v['up_time']);
        }
        if($get!=0){
            $this->info['code']=1;
            $this->info['msg']='成功';
            $this->info['data']=$listData;
            return json($this->info);
        }else{
            $typeList = Db::table('an_type')->where('type', 'news')->order('id', 'desc')->select()->toArray();
            $this->assign('typeList',$typeList);
            $this->assign('listData',json_encode($listData));
            $this->assign('totalNum',$totalNum);
            return $this->fetch();
        }   
    }

    //删除
    public function del_data(){
        $pt = input("post.");
        $bool = Db::table('tb_news')->where('id',$pt['id'])->delete();
        $this->logData('删除新闻资讯:'.$pt['id']);
        $this->info['code']=1;
        return json($this->info);
    }

    //排序
    public function sort_data(){
        $pt = input("post.");
        $bool = Db::table('tb_news')->where('id', $pt['id'])->data($pt)->update();;
        $this->info['code']=1;
        $this->info['msg']='成功';
        return json($this->info);
    }

    //排序
    public function disable_data(){
        $pt = input("post.");
        $bool = Db::table('tb_news')->where('id', $pt['id'])->data($pt)->update();;
        $this->info['code']=1;
        $this->info['msg']='成功';
        return json($this->info);
    }

    //编辑数据
    public function form_data($id=0){
        $pt = input("post.");
        if(count($pt)>0){
            $pt['up_time'] = time();
            $bool = Db::table('tb_news')->where('id', $pt['id'])->data($pt)->update();
            $this->logData('修改新闻资讯:'.$pt['title']);
            if($bool == 1){
                $this->info = ['code'=>1,'msg'=>'操作成功','data'=>$pt];
            }else{
                $this->info = ['code'=>0,'msg'=>'操作失败','data'=>$pt];
            }
            return json($this->info);
        }
        if($id!=0){
            $pageData = Db::table('tb_news')->where('id', $id)->find();
        }else{
            $pageData = [
                'title'=>'',
                'cover'=>'',
                'sort'=>'',
                'content'=>'',
                'abstract'=>'',
                'is_show'=>'',
                'type'=>'',
                'seo_title'=>'',
                'seo_keywords'=>'',
                'seo_description'=>'',
                'attr_a'=>false,
                'attr_b'=>false,
                'attr_c'=>false,
            ];
        }
        $this->assign('pageData',$pageData);
        $typeList = Db::query("select * from an_type where type=2");
        $this->assign('typeList',$typeList);
        $this->assign('id',$id);
        return $this->fetch();
    }

    //添加数据
    public function form_add_data(){
        $pt = input("post.");
        $pt['create_time'] = time();
        $pt['up_time'] = time();
        $bool = Db::table('tb_news')->insert($pt);
        $this->logData('新增新闻资讯:'.$pt['title']);
        if($bool == 1){
            $this->info = ['code'=>1,'msg'=>'操作成功','data'=>$pt];
        }else{
            $this->info = ['code'=>0,'msg'=>'操作失败','data'=>$pt];
        }
        return json($this->info);
    }
    


}
